package com.qilin.hammer;

import static com.qilin.hammer.util.Global.ORCLE_DATABASE;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.qilin.hammer.util.database.DBConnection;
import com.qilin.hammer.util.database.DBFactory;
import com.qilin.hammer.util.database.DBOperator;

public class DBData_Creator {
	
	private static int colnum = 5;
	private static int rownum = 81*3;
	private static String head ="combo_";
	private static String[] end = {"1","2","3"};
	
	private int flags[];
	
	
	{
		this.flags = new int[colnum];
		for(int i=0;i<colnum;i++){
			//i==0?flags[i]=rownum:flags[i]=flags[i-1]/3;
			if(i==0){
				flags[i]= 10*10*10*10;
			}else{
				flags[i]=flags[i-1]/10;
			}
		}
	}
	
	public DBData_Creator(){}
	
//	public ArrayList<ArrayList<String>> getStrData(){
//		ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
//		for(int i=0;i<colnum;i++){
//			data.add(new ArrayList<String>(rownum));
//			int current = -1;
//			for(int j=0; j<rownum; j++){
//				if (j % this.flags[i]==0)
//					current++;
//				if(current==3)
//					current = 0;
//				data.get(i).add(i==0?head+end[current]:data.get(i-1).get(j)+"."+end[current]);			
//			}
//		}
//		return data;
//	}
	
	public ArrayList<ArrayList<String>> getStrData(){
		int rownum = 10*10*10*10*10;
		int colnum = 5;
		String[] end = new String[10];
		for(int i=0;i<10;i++)
			end[i]=String.valueOf(i+1);
		ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
		for(int i=0;i<colnum;i++){
			data.add(new ArrayList<String>(rownum));
			int current = -1;
			for(int j=0; j<rownum; j++){
				if (j % this.flags[i]==0)
					current++;
				if(current==10)
					current = 0;
				data.get(i).add(i==0?head+end[current]:data.get(i-1).get(j)+"."+end[current]);			
			}
		}
		return data;
	}
	
	public ArrayList<ArrayList<String>> getDateData(){
		ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
		int colnum = 4;
		int rownum = 12*12*9;
		String dates [] = new String[9];
		String times [] = new String[12];
		String datetimes[] = new String[12*9];
		String products [] = new String[12];
		int flags[] = {12*12,12,12,1};
		int currents[] = {9,12,12*9,12};
		//SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		for(int i=1;i<10;i++)
			dates[i-1]="2011-08-0"+i;
		for(int i=0;i<12;i++)
			times[i]=(12+i)+":00:00";
		int count = 0;
		for(int i=0;i<9;i++){
			for(int j=0;j<12;j++){
				datetimes[count++]=dates[i]+" "+times[j];
			}	
		}
		for(int i=0;i<12;i++){
			products[i] = "product"+(i+1);
		}
		for(int i=0;i<colnum;i++){
			data.add(new ArrayList<String>(rownum));
			int current = -1;
			for(int j=0; j<rownum; j++){
				if (j % flags[i]==0)
					current++;
				if(current == currents[i])
					current = 0;
				switch(i){
					case 0:
						data.get(i).add(dates[current]);
						break;
					case 1:
						data.get(i).add(times[current]);
						break;
					case 2:
						data.get(i).add(datetimes[current]);
						break;
					case 3:
						data.get(i).add(products[current]);
						break;
				}		
			}
		}
		return data;
	}
	
	
	public void createStrData(DBOperator orcloperator){
		ArrayList<ArrayList<String>> data = this.getStrData();
    	orcloperator.execute("delete from FORM_TEST_STR_L");
		for(int i=0;i<10*10*10*10*10;i++){
			String sql="insert into FORM_TEST_STR_L values (";
			for(int j=0;j<5;j++){
				sql+="'"+data.get(j).get(i)+"',";
			}
			sql=sql.substring(0, sql.length()-1);
			sql+=")";
			System.out.println(sql);
			orcloperator.execute(sql);
		}
	}
	
	public void createDateData(DBOperator orcloperator){
		ArrayList<ArrayList<String>> data = this.getDateData();
		orcloperator.execute("delete from form_test_date");
		for(int i=0;i<12*12*9;i++){
			String sql="insert into form_test_date values (";
			for(int j=0;j<4;j++){
				if(j==2){
					sql+="to_date('"+data.get(j).get(i)+"','yyyy-mm-dd HH24:MI:SS'),";
					continue;
				}
				sql+="'"+data.get(j).get(i)+"',";
			}
			sql=sql.substring(0, sql.length()-1);
			sql+=")";
			System.out.println(sql);
			orcloperator.execute(sql);
		}
	}
		
	
	public void createLargeData(DBOperator orcloperator, int size){
		orcloperator.execute("delete from form_test_large_policy");
		String head = "insert into form_test_large_policy values (";
		int current = 0;
		Random r = new Random();
		for(int i=0;i<500;i++){
			String sql = head;
			if(i%10==0)
				current++;
			sql+="'combo_"+current+"','combo_"+current+"."+(i%10+1)+"')";
			System.out.println(sql);
			orcloperator.execute(sql);
		}
		String table = "create table \"ZHANGRM\".\"FORM_TEST_LARGE_DATA\" (";
		for(int i=0; i<100; i++){
			table += "\"FIELD"+(i+1)+"\" VARCHAR2(16) NOT NULL,";
		}
		table = table.substring(0, table.length()-1);
		table+=")";
		System.out.println(table);
		orcloperator.execute(table);
		head = "insert into form_test_large_data values (";
		for(int i=0; i<size; i++){
			String sql = head;
			for(int j=1; j<=50; j++){
				int index = r.nextInt(50)+1;
				sql += "'combo_"+index+"','combo_"+index+"."+(r.nextInt(10)+1)+"',";			
				}
			sql = sql.substring(0, sql.length()-1);
			sql+=")";
			System.out.println(sql);
			orcloperator.execute(sql);
		}
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DBFactory dbfactory = new DBFactory();
    	DBConnection orclconnector=dbfactory.getDBConnectionInstance(ORCLE_DATABASE);
    	Connection orclconn = orclconnector.getConnection("10.111.3.50", "1521", "ZHANGRM", "123456");
    	DBOperator orcloperator=new DBOperator(orclconn);
    	DBData_Creator creator = new DBData_Creator();
    	//creator.createStrData(orcloperator);
    	creator.createDateData(orcloperator);
    	//creator.createLargeData(orcloperator,1000);
    	//for(int i=0;i<100;i++)
    		//System.out.println("<xs:element minOccurs=\"0\" nillable=\"true\" name=\"name"+(i+1)+"\" type=\"xs:string\"/>");
	}

}
